<template>
  <div class="container">
    <template v-for="(item, index) in form">
    <el-form ref="form" :model="item" label-width="110px">
        <el-form-item label="标题文字" v-if="item.isTitle" :key="index">
          <el-input v-model="item.title"></el-input>
        </el-form-item>
        <div class="model-item" v-else>
          <div class="head">
            <span>模块{{modelIndex[index-1]}}</span>
          </div>
          <div style="padding-right:16px">
            <el-form-item label="模块图标">
              <el-upload
              action=""
              list-type="picture-card"
              :http-request="(file)=>handleCustomUpload(file,index)"
              :show-file-list="false"
            >
              <div class="modelBox" v-if="item.url.url" @click.stop>
                <div class="model">
                  <i class="el-icon-zoom-in" @click="handlePictureCardPreview(item)"></i>
                  <i class="el-icon-delete" @click="handleDeleteUpload(item)"></i>
                </div>
                <img  :src="item.url.url" class="el-upload--picture-card" />
              </div>
              <i slot="default" class="el-icon-plus" v-else></i>
            </el-upload>
            <div class="tips">请上传png格式，尺寸建议为240px*130px</div>
            </el-form-item>
            <el-form-item label="模块标题">
              <el-input v-model="item.title"></el-input>
            </el-form-item>
            <el-form-item label="中文名称">
              <el-input v-model="item.subititle2"></el-input>
            </el-form-item>
            <el-form-item label="模块内容">
              <el-input v-model="item.subtitle" type="textarea" :rows="3"></el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </template>
    <div class="save">
      <el-button type="primary" @click="handleSave" style="margin-top:16px;">保存</el-button>
    </div>
    <el-image-viewer
      v-if="isShowImgView"
      :url-list="dialogImageUrl"
      :initial-index="0"
      :on-close="closeViewer"
      :z-index="3000"
    />
  </div>
</template>
  <script>
import { arraySort } from "@/utils";
import naTable from "@/components/naTable";
import { mapGetters } from "vuex";
export default {
  name: "cartonConfig",
  components: {
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
  },
  data() {
    return {
      form:{
      },
      dialogImageUrl:[],
      isShowImgView:false,
      modelIndex:['一','二','三']
    };
  },
  computed: {
    ...mapGetters(["name"]),
  },
  created() {
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData(){
        this.$api.system.GetList(1).then((res)=>{
        this.form = res
        })
      },

    handleSave(){
      this.$api.system.UpdateMore(this.form).then(res=>{
          this.$message.success('保存成功！')
          this.getData()
        })
    },

    handleCustomUpload(e,index){
      const file = e.file;
      const type = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (
        type !== "png" &&
        type !== "jpg" &&
        type !== "jpeg" &&
        type !== "gif" &&
        type !== "bmp"
      ) {
        this.$message.error("上传图片格式错误，请重新上传!");
        return false;
      } else {
          var formData = new FormData();
            formData.append("file", file);
            formData.append("output", 'json2');
            this.$api.system.upload(formData).then((res) => {
              let prefix = '/assets'
              this.form[index].url.url = prefix+res.src
              this.$message.success("上传成功！");
            });
      }
    },

    //预览图片
    handlePictureCardPreview(row){
      this.dialogImageUrl = []
      this.dialogImageUrl.push(row.url.url)
      this.isShowImgView = true
    },

    //删除图片
    handleDeleteUpload(row){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          row.url.url = ''
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
        });
      },
    closeViewer(){
      this.isShowImgView = false
    }
  },
};
</script>
  <style lang="scss" scoped>
.container{
  padding:16px;
  overflow-y:auto;
  height:100%;
  $size:148px;
  .tips{
    font-size:13px;
    color:rgba(153, 153, 153, 1);
  }

  .save{
    display:flex;
    justify-content:center;
  }
  .modelBox{
    .model{
      display: none;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      width:$size;
      height:$size;
      border-radius:6px;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 2;
      >*{
        font-size: 20px;
        color: #fff;
        &:hover{
          color: #3f61f1;
        }
        &:not(:last-child){
          margin-right: 20px;
        }
      }
    }
    &:hover{
      .model{
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .el-upload{
    width: #{$size};
    height: #{$size};
    position: relative;
    .tag{
      position: absolute;
      left: 0;
      top: 0;
      padding: 4px 6px;
      line-height: 1.2em;
      background: rgba(0, 0, 0, 0.75);
      color: #fff;
      font-size: 12px;
      border-radius: 6px 0 4px 0;
      z-index: 2;
    }
  }
  .img_box{
    width: #{$size};
    height: #{$size};
    position: relative;
  }
  .img_boxMore{
    display: flex;
    flex-wrap: wrap;
    margin-right: -8px;
    margin-bottom: -8px;
    .img_box{
      border-radius:8px;
      border: 1px #dcdfe6 solid;
      overflow: hidden;
      margin-right: 8px;
    }
    >*{
      margin-right: 8px;
      margin-bottom: 8px;
    }
  }
}
  .model-item{
    margin-top:10px;
    border:1px solid #e8e8e8;
    .head{
      margin-bottom:16px;
      padding-left:30px;
      display:flex;
      align-items:center;
      height:40px;
      background: rgba(253, 240, 255, 1);
    }
  }
</style>
  